package pt.isel.pdm.yamba;
import winterwell.jtwitter.Twitter;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class UserInfoPull extends Service{
	
	public class UserInfoOpers extends IUserInfoOpers.Stub {
		private String _username = "";
		private String _password = "";
		private String _apiuri = "";
		private Twitter _twitter = null;
		private void Assert(String username, String password, String apiuri){
			try{
				if(!_username.equals(username) || !_password.equals(password) || !_apiuri.equals(apiuri)){
					_username = username;
					_password = password;
					_apiuri = apiuri;
					_twitter = new Twitter(_username, _password);
					_twitter.setAPIRootUrl(_apiuri);
				}
			}catch(Exception e){
				Log.d("PDM", e.getMessage());
			}
		}
		public String getUserName(String username, String password, String apiuri) {
			Assert(username, password, apiuri);
			return _twitter.getScreenName();
		}
		public String getUserImage(String username, String password, String apiuri){
			Assert(username, password, apiuri);
			try {
				return _twitter.getUser(_username).getProfileImageUrl().toURL().toString();
			} catch (Exception e) {
				return null;
			}
		}
		public int getUserMessages(String username, String password, String apiuri){
			Assert(username, password, apiuri);
			return _twitter.getUser(_username).getStatusesCount();
		}
		public int getUserFollowing(String username, String password, String apiuri){
			Assert(username, password, apiuri);
			return _twitter.getUser(_username).getFriendsCount();
		}
		public int getUserFollowers(String username, String password, String apiuri){
			Assert(username, password, apiuri);
			return _twitter.getUser(_username).getFollowersCount();
		}
	}
	public UserInfoOpers opers;
	@Override
	public IBinder onBind(Intent arg0) {
		if(opers == null)
			opers = new UserInfoOpers();
		return opers;
	}
}
